package com.shenmazong.servereduresume.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.servereduresume.dto.TbLevel;
import com.shenmazong.servereduresume.dto.TbSrwp;
import com.shenmazong.servereduresume.dto.TbUserSrwp;
import com.shenmazong.servereduresume.service.TbUserSrwpService;
import com.shenmazong.servereduresume.mapper.TbUserSrwpMapper;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.BatchChooseIdVo;
import com.shenmazong.vo.ListParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class TbUserSrwpServiceImpl extends ServiceImpl<TbUserSrwpMapper, TbUserSrwp>
implements TbUserSrwpService{

    @Autowired
    private TbUserSrwpMapper tbUserSrwpMapper;

    @Override
    public ResultResponse getMySrwp(ListParamVo listParamVo) {

        Page<TbSrwp> page = new Page<>(listParamVo.getCurrent(), listParamVo.getSize());
        IPage<TbSrwp> pageList = tbUserSrwpMapper.getMySrwp(page,listParamVo.getParamMap());
        return ResultResponse.SUCCESS(pageList);
    }

    @Override
    public ResultResponse saveMySrwp(TbUserSrwp userSrwp) {
        TbUserSrwp one = tbUserSrwpMapper.selectOne(new QueryWrapper<TbUserSrwp>().lambda().eq(TbUserSrwp::getSrwpId, userSrwp.getSrwpId()).eq(TbUserSrwp::getUserId, userSrwp.getUserId()));
        if (one != null){
            return ResultResponse.SUCCESS("-1");
        }
        try {
            tbUserSrwpMapper.insert(userSrwp);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS("200");
    }

    @Override
    public ResultResponse deleteMySrwp(BatchChooseIdVo batchChooseIdVo) {
        if (batchChooseIdVo.getIds().size() <= 0){
            return ResultResponse.FAILED("至少收藏一个简历");
        }
        try {
            for (Long id : batchChooseIdVo.getIds()) {
                UpdateWrapper<TbUserSrwp> updateWrapper = new UpdateWrapper<TbUserSrwp>().eq("srwp_id", id).eq("user_id",batchChooseIdVo.getParamMap().get("userId"));
                updateWrapper.set("delete_flag",1);
                tbUserSrwpMapper.delete(updateWrapper);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }
}




